package com.boot.controller.auth;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.boot.ao.auth.SignupAO;
import com.boot.common.po.Result;
import com.boot.entity.SysUser;
import com.boot.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/user")
@Api(value = "用户管理", tags = "用户管理", description = "用户管理--接口规范")
public class UserController extends BaseController {

    @Resource
    private SysUserService sysUserService;


    @PostMapping("/signup")
    @ApiOperation(value = "注册用户", notes = "注册用户", tags = "用户管理")
    public Result signup(@RequestBody SignupAO signupAO) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", signupAO.getUsername());
        SysUser sysUser = sysUserService.getOne(queryWrapper);
        if(null != sysUser){
            logger.error("用户已经存在");
//            throw new UsernameIsExitedException("用户已经存在");
        }else
            sysUser = new SysUser();
        /*user.setPassword(DigestUtils.md5DigestAsHex((user.getPassword()).getBytes()));*/
        //sysUser.setPassword(bCryptPasswordEncoder.encode(sysUser.getPassword()));
        BeanUtils.copyProperties(signupAO,sysUser);
        sysUserService.insert(sysUser);
        return Result.success();
    }

    @GetMapping("/userList")
    @ApiOperation(value = "查询用户列表", notes = "查询用户列表", tags = "用户管理")
    public Result userList(){
        List<SysUser> users = sysUserService.getList();
        logger.info("users: {}", users);
        return Result.success(users);
    }

//    @GetMapping("/authorityList")
//    @ApiOperation(value = "查询用户权限", notes = "查询用户权限", tags = "用户管理")
//    public Result authorityList(){
//        List<String> authentication = getAuthentication();
//        return Result.success(authentication);
//    }
}
